package com.hc.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hc.entity.House;
import com.hc.service.HouseService;
import com.hc.serviceimp.HouseServiceImpl;


@WebServlet("/SelectHouseServlet")
public class SelectHouseServlet extends HttpServlet {
	
	HouseService hService = new HouseServiceImpl();

    public SelectHouseServlet() {
        super();
      
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		//跳转查询页面
		if (action.equals("tolist")) {
			
			request.getRequestDispatcher("/WEB-INF/house/tolist.jsp").forward(request, response);
		}
		//跳转显示页面
		if (action.equals("list")) {
			String dq = request.getParameter("diqu");
			String xq = request.getParameter("xiaoqu");
			int mj = Integer.parseInt(request.getParameter("mianji"));
			int jg=Integer.parseInt(request.getParameter("jiage"));
			String zt = request.getParameter("zhuangtai");
			List<House> houses = hService.pageSelect(dq, xq, mj, jg, zt);
			request.setAttribute("house", houses);
			request.getRequestDispatcher("/WEB-INF/house/list.jsp").forward(request, response);
		}
		//显示详细信息页面
		if (action.equals("detail")) {
			int id = Integer.parseInt(request.getParameter("hid"));
			
			House house = hService.getDetails(id);
			
			request.setAttribute("h", house);
			request.getRequestDispatcher("/WEB-INF/house/details.jsp").forward(request, response);
			
		}
		//跳转修改页面
		if (action.equals("toedit")) {
			int id = Integer.parseInt(request.getParameter("hid"));
			
			House house = hService.getDetails(id);
			
			request.setAttribute("h", house);
			request.getRequestDispatcher("/WEB-INF/house/edit.jsp").forward(request, response);
		}
		//修改信息
		if (action.equals("update")) {
			House house=null;
			String id = request.getParameter("hid");
			String dq = request.getParameter("dq");
			String xq = request.getParameter("xq");
			String dy = request.getParameter("dy");
			String lc = request.getParameter("lc");
			String fj = request.getParameter("fj");
			String mj = request.getParameter("mj");
			String cx = request.getParameter("cx");
			String zx = request.getParameter("zx");
			String sq = request.getParameter("sq");
			String xz = request.getParameter("xz");
			String ss = request.getParameter("ss");
			String jg = request.getParameter("jg");
			String zt = request.getParameter("zt");
			String wz = request.getParameter("wz");
			String tj = request.getParameter("tj");
			String gx = request.getParameter("gx");
			house = new House(Integer.parseInt(id), dq, xq, Integer.parseInt(dy), Integer.parseInt(lc), Integer.parseInt(fj), Integer.parseInt(mj), cx, zx, sq, Integer.parseInt(xz), ss, Integer.parseInt(jg), zt, wz, tj, gx);
						
			hService.update(house);
			System.out.println("更新完成");
			request.getRequestDispatcher("SelectHouseServlet?action=show").forward(request, response);
		}
		
		if (action.equals("show")) {
			
			List<House> show = hService.show();
			request.setAttribute("house", show);
			
			request.getRequestDispatcher("/WEB-INF/house/list.jsp").forward(request, response);
		}
		if (action.equals("del")) {
			
			int id= Integer.parseInt(request.getParameter("hid"));
			hService.delete(id);
			request.getRequestDispatcher("SelectHouseServlet?action=show").forward(request, response);
		}
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}
	
}
